<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix header-box">
        <span>文章分类</span>
        <el-button @click="dialogVisible = true" type="warning" size="mini" round>添加分类</el-button>
      </div>
      <template>
        <el-table :data="cateList" style="width: 100%" stripe>
          <el-table-column type="index" label="序号" width="180">
          </el-table-column>
          <el-table-column prop="cate_name" label="分类名称" width="180">
          </el-table-column>
          <el-table-column prop="cate_alias" label="分类别名">
          </el-table-column>
          <el-table-column label="操作">
            <template v-slot="{row}">
              <el-button @click="popEditForm(row)" size="mini" type="primary" icon="el-icon-edit" circle></el-button>
              <el-button @click="remove(row.id)" size="mini" type="danger" icon="el-icon-delete" circle></el-button>
            </template>
          </el-table-column>
        </el-table>
      </template>
      <!-- 添加分类弹框 -->
      <el-dialog class="dialog-form" :close-on-click-modal="false" title="添加文章分类" :visible.sync="dialogVisible" width="35%" @closed="closedDialog">
        <!-- 添加表单 -->
        <el-form ref="addRef" :model="addFormData" :rules="addFormDataRules" label-width="70px">
          <el-form-item label="分类名称" prop="cate_name">
            <el-input v-model="addFormData.cate_name" minlength="1" maxlength="10"></el-input>
          </el-form-item>
          <el-form-item label="分类别名" prop="cate_alias">
            <el-input v-model="addFormData.cate_alias" minlength="1" maxlength="15"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button size="mini" @click="dialogVisible = false">Cancel</el-button>
          <el-button size="mini" type="primary" @click="postCateList">Confirm</el-button>
        </span>
      </el-dialog>
      <!-- 修改分类弹框 -->
      <el-dialog :close-on-click-modal="false" title="修改文章分类" :visible.sync="dialogFormVisible" width="400px">
        <el-form ref="editFormRef" :rules="addFormDataRules" :model="editForm">
          <el-form-item label="分类名称" label-width="80px" prop="cate_name">
            <el-input v-model="editForm.cate_name"></el-input>
          </el-form-item>
          <el-form-item label="分类别名" label-width="80px" prop="cate_alias">
            <el-input v-model="editForm.cate_alias"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="editCate">确 定</el-button>
        </div>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
import { getCateListAPI, addCateListAPI, deleteCateListAPI, editCateFoemAPI } from '@/api/article.js'
export default {
  name: 'ArtCate',
  data () {
    return {
      // 文章的分类列表
      cateList: [],
      // 添加文章分类弹框控制
      dialogVisible: false,
      // 添加文章数据
      addFormData: {
        cate_alias: '',
        cate_name: '',
        id: ''
      },
      // 正则  addFormData
      addFormDataRules: {
        cate_alias: [
          { requierd: true, message: '请输入分类别名', trigger: 'blur' },
          { pattern: /^[a-zA-Z0-9]{1,15}$/, message: '分类别名必须是1-15位的字母数字', trigger: 'blur' }
        ],
        cate_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' },
          { pattern: /^\S{1,10}$/, message: '分类名必须是1-10位的非空字符', trigger: 'blur' }
        ]

      },
      // 修改文章数据
      dialogFormVisible: false,
      editForm: {
        cate_name: '',
        cate_alias: '',
        id: ''
      }
    }
  },
  methods: {
    // method 获取文章列表
    async initCate () {
      const { data: res } = await getCateListAPI()
      // console.log(res)
      if (res.code === 0) {
        // this.$message.success(res.message)
        this.cateList = res.data
        // console.log(this.cateList)
      } else {
        this.$message.error(res.message)
      }
    },
    // 关闭 dialog 框, 重置表单
    closedDialog () {
      this.$refs.addRef.resetFields()
    },
    // 添加文章分类
    async postCateList () {
      // this.dialogVisible = false
      // 表单预校验
      this.$refs.addRef.validate(async valid => {
        if (!valid) return
        // TODO：发起请求，添加分类
        const { data: res } = await addCateListAPI(this.addFormData)
        // console.log(res)
        if (res.code === 0) {
          this.$message.success(res.message)
          this.dialogVisible = false
          this.initCate()
          this.$refs.addRef.resetFields()
        } else {
          this.$message.error(res.message)
          this.$refs.addRef.resetFields()
        }
      })
    },
    // 点击修改弹出 diolog 框
    popEditForm (row) {
      if (row.id === 1 || row.id === 2) return this.$message.error('管理员不允许修改 id 为 1（最新） 和 2（科技） 的分类！')
      this.editForm = Object.assign({}, row)
      this.dialogFormVisible = true
    },
    // 修改文章
    async editCate () {
      // console.log(id)
      try {
        const result = await this.$refs.editFormRef.validate()
        if (result === false) return
        const { data: res } = await editCateFoemAPI(this.editForm)
        // console.log(res)
        if (res.code === 0) {
          this.$message.success(res.message)
          this.dialogFormVisible = false
          this.initCate()
        } else {
          this.$message.error(res.message)
          // this.dialogFormVisible = false
          this.initCate()
        }
      } catch (error) {
        return console.log(error)
      }
      this.$refs.editFormRef.resetFields()
    },
    // 删除
    async remove (id) {
      // console.log(id)
      if (id === 1 || id === 2) return this.$message.error('管理员不允许删除 id 为 1（最新） 和 2（科技） 的分类！')
      try {
        const result = await this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        if (result === 'cancel') return
        const { data: res } = await deleteCateListAPI(id)
        // console.log(res)
        if (res.code === 0) {
          this.$message.success(res.message)
          this.initCate()
        } else {
          this.$message.error(res.message)
        }
      } catch (error) {
        console.log(error)
        this.$message.error(error)
      }
    }
  },
  created () {
    this.initCate()
  }
}
</script>

<style lang="less" scoped>
.header-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
